import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpClient} from "@angular/common/http";
import {AppConfig} from "../../../app/appConfig";
import {CommonService} from "../../../app/common.service";
import {AppUrl, TIME_OUT} from "../../../app/app.general";

/**
 * Generated class for the MyRestaurantPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-my-restaurant',
  templateUrl: 'my-restaurant.html',
})
export class MyRestaurantPage {
  private restaurant = {
    rName: '',
    rAddr: '',
    tel: '',
    certifyImgId: '',
    rId: 0,
    workrange: '',
    IId: ''
  };
  private upload = false;
  private image = {//图片请求数据绑定
    imagePrefix: AppUrl.image.getById,
    type: AppUrl.image.type.restaurant,
  };
  private oldImgId;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private httpService: HttpClient,
              private appConfig: AppConfig,
              private cs: CommonService) {
    this.appConfig.presentLoading("获取店铺数据中");
    this.httpService.get(AppUrl.restaurant.myRestaurant).timeout(TIME_OUT).subscribe(data => {
      this.appConfig.loader.dismiss();
      let temp: any = data;
      if (temp.code == 200) {
        this.restaurant = temp.data;
        if (this.restaurant.IId != '') {
          this.oldImgId = this.restaurant.IId;
        }
      }
      else {
        this.appConfig.presentToast(temp.msg);
      }
    }, e => {
      this.appConfig.loader.dismiss();
      this.cs.httpError(e);
    })
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MyRestaurantPage');
  }

  change(imgId: string) {
    if (imgId) {
      this.httpService.post(AppUrl.restaurant.updateRestaurant, {
        id: this.restaurant.rId,
        tel: this.restaurant.tel,
        addr: this.restaurant.rAddr
      }).timeout(TIME_OUT).subscribe(data => {
        let temp: any = data;
        if (temp.code == 200) {
          this.appConfig.presentToast('修改成功');
          this.navCtrl.pop();
        }
        else {
          this.appConfig.presentToast(temp.msg);
        }
      }, e => {
        this.cs.httpError(e);
      })
    }
  }

  save() {
    this.upload = true;
  }
}
